package com.microframework.boot.visual.controller;

import java.sql.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jfinal.kit.StrKit;
import com.microframework.base.core.constants.CacheConstants;
import com.microframework.base.core.model.options.TreeNode;
import com.microframework.base.core.util.SecurityUtils;
import com.microframework.base.core.util.UuidUtil;
import com.microframework.base.web.page.PageQuery;
import com.microframework.base.web.resp.R;
import com.microframework.boot.visual.model.domain.ComponentCategory;
import com.microframework.boot.visual.service.IComponentCategory;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/visual/component/category")
@Tag(name = "组件分类管理", description = "组件分类管理相关接口")
public class ComponentCategoryController {

    @Autowired
    private IComponentCategory componentCategoryService;
    
    @PostMapping("/load/all")
    @Operation(summary = "一次性加载树型数据", description = "一次性加载树型数据")
    public R<List<ComponentCategory>> loadAll(@RequestBody PageQuery<ComponentCategory> pageQuery) {
        return R.ok(componentCategoryService.loadAll(pageQuery));
    }
    
    @PostMapping("/load/lazy")
    @Operation(summary = "懒加载树型数据", description = "懒加载树型数据")
    public R<List<ComponentCategory>> loadLazy(@RequestBody PageQuery<ComponentCategory> pageQuery) {
        return R.ok(componentCategoryService.loadLazy(pageQuery));
    }
    
    
    @PostMapping("/select/tree")
    @Operation(summary = "树形下拉框", description = "树形下拉框数据")
    public R<List<TreeNode<ComponentCategory>>> tree(@RequestBody PageQuery<ComponentCategory> pageQuery) {
        return R.ok(componentCategoryService.tree(pageQuery));
    }
    
	@Operation(summary = "保存组件分类")
	@Parameters({ @Parameter(name = "componentCategory", description = "组件分类", required = true) })
	@PostMapping("/save")
	@CachePut(value = CacheConstants.VISUAL_CACHES, key = "#componentCategory.id")
	public R<ComponentCategory> save(HttpServletRequest request, @RequestBody ComponentCategory componentCategory) {
		try {
			if (StrKit.notBlank(componentCategory.getId())) {
				componentCategory.setModifyTime(new Date(System.currentTimeMillis()));
				componentCategory.setModifyUserId(SecurityUtils.getUserId());
				if (componentCategory.update()) {
					return R.ok(componentCategory);
				} else {
					return R.fail("更新失败.");
				}
			} else {
				componentCategory.setId(UuidUtil.getUUID());
				componentCategory.setCreateTime(new Date(System.currentTimeMillis()));
				componentCategory.setCreateUserId(SecurityUtils.getUserId());
				if (componentCategory.save()) {
					return R.ok(componentCategory);
				} else {
					return R.fail("保存失败.");
				}
			}
		} catch (Exception e) {
			return R.fail(e.getMessage());
		}
	}
	
	@Operation(summary = "删除组件分类")
	@Parameters({ @Parameter(name = "id", description = "组件分类id", required = true) })
	@DeleteMapping("/detain/{id}")
	@CacheEvict(value = CacheConstants.VISUAL_CACHES, key = "#id")
	public R<String> deleteById(@PathVariable("id") String id) {
		try {
			ComponentCategory componentCategory=new ComponentCategory();
			componentCategory.setId(id);
			componentCategory.setIsDel("1");
			if(componentCategory.update()) {
				return R.ok("删除成功.");
			}else {
				return R.fail("删除失败.");
			}
		}catch (Exception e) {
			log.error("删除异常:{}",e.getMessage());
			return R.fail(e.getMessage());
		}
	}
} 